﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        Point scrollMousePoint = new Point();
        double hOff = 1;
        double vOff = 1;

        private void scrollViewer_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            scrollMousePoint = e.GetPosition(scrollViewer);

            if (scrollMousePoint.X > scrollViewer.Width - 10)
                return;

            if (scrollMousePoint.Y > scrollViewer.Height - 10)
                return;

            hOff = scrollViewer.HorizontalOffset;
            vOff = scrollViewer.VerticalOffset;

            scrollViewer.CaptureMouse();
        }

        private void scrollViewer_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (scrollViewer.IsMouseCaptured)
            {   
                scrollViewer.ScrollToHorizontalOffset(hOff + (scrollMousePoint.X - e.GetPosition(scrollViewer).X));
                scrollViewer.ScrollToVerticalOffset(vOff + (scrollMousePoint.Y - e.GetPosition(scrollViewer).Y));
            }
            
        }

        private void scrollViewer_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            scrollViewer.ReleaseMouseCapture();
        }
        
        
        private void scrollViewer_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var currentScrollPositionV = scrollViewer.VerticalOffset;
            var currentScrollPositionH = scrollViewer.HorizontalOffset;

            var point = new Point(currentScrollPositionH, currentScrollPositionV);

            var targetPosition = e.GetPosition(scrollViewer);


            scrollViewer.ScrollToVerticalOffset(targetPosition.Y - point.Y);
            scrollViewer.ScrollToHorizontalOffset(targetPosition.X - point.X);

        }


    }
}
